{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Домашнее задание к лекции \"Базовые понятия статистики\"\n",
    "\n",
    "Обязательная часть\n",
    "\n",
    "Будем осуществлять работу с непростым набором данных о состоянии здоровья лошадей, испытывающих кишечные колики."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from collections import Counter\n",
    "import random\n",
    "from copy import deepcopy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 surgery\n",
      "2 age\n",
      "3 hospital_number\n",
      "4 rectal_temperature\n",
      "5 pulse\n",
      "6 respiratory_rate\n",
      "7 temperature_of_extremities\n",
      "8 peripheral_pulse\n",
      "9 mucous_membranes\n",
      "10 capillary_refill_time\n",
      "11 pain\n",
      "12 peristalsis\n",
      "13 abdominal_distension\n",
      "14 nasogastric_tube\n",
      "15 nasogastric_reflux\n",
      "16 nasogastric_reflux_PH\n",
      "17 rectal_examination\n",
      "18 abdomen\n",
      "19 packed_cell_volume\n",
      "20 total_protein\n",
      "21 abdominocentesis_appearance\n",
      "22 abdomcentesis_total_protein\n",
      "23 outcome\n",
      "24 surgical_lesion\n",
      "25 type_of_lesion_1\n",
      "26 type_of_lesion_2\n",
      "27 type_of_lesion_3\n",
      "28 cp_data\n"
     ]
    }
   ],
   "source": [
    "# Создаем список названий колонок для будущего фрейма с лошадьми\n",
    "df_horses_col_names = [\n",
    "    'surgery',\n",
    "    'age',\n",
    "    'hospital_number',\n",
    "    'rectal_temperature',\n",
    "    'pulse',\n",
    "    'respiratory_rate',\n",
    "    'temperature_of_extremities',\n",
    "    'peripheral_pulse',\n",
    "    'mucous_membranes',\n",
    "    'capillary_refill_time',\n",
    "    'pain',\n",
    "    'peristalsis',\n",
    "    'abdominal_distension',\n",
    "    'nasogastric_tube',\n",
    "    'nasogastric_reflux',\n",
    "    'nasogastric_reflux_PH',\n",
    "    'rectal_examination',\n",
    "    'abdomen',\n",
    "    'packed_cell_volume',\n",
    "    'total_protein',\n",
    "    'abdominocentesis_appearance',\n",
    "    'abdomcentesis_total_protein',\n",
    "    'outcome',\n",
    "    'surgical_lesion',\n",
    "    'type_of_lesion_1',\n",
    "    'type_of_lesion_2',\n",
    "    'type_of_lesion_3',\n",
    "    'cp_data'    \n",
    "]\n",
    "# Сверяемся с файлом horse_data.names\n",
    "for i, k in enumerate(df_horses_col_names, 1):\n",
    "    print(i, k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "      <th>mucous_membranes</th>\n",
       "      <th>capillary_refill_time</th>\n",
       "      <th>...</th>\n",
       "      <th>packed_cell_volume</th>\n",
       "      <th>total_protein</th>\n",
       "      <th>abdominocentesis_appearance</th>\n",
       "      <th>abdomcentesis_total_protein</th>\n",
       "      <th>outcome</th>\n",
       "      <th>surgical_lesion</th>\n",
       "      <th>type_of_lesion_1</th>\n",
       "      <th>type_of_lesion_2</th>\n",
       "      <th>type_of_lesion_3</th>\n",
       "      <th>cp_data</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>530101</td>\n",
       "      <td>38.5</td>\n",
       "      <td>66.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>45.0</td>\n",
       "      <td>8.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>11300</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>534817</td>\n",
       "      <td>39.2</td>\n",
       "      <td>88.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>50.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2208</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>530334</td>\n",
       "      <td>38.3</td>\n",
       "      <td>40.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>33.0</td>\n",
       "      <td>6.7</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5290409</td>\n",
       "      <td>39.1</td>\n",
       "      <td>164.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>48.0</td>\n",
       "      <td>7.2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2208</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>530255</td>\n",
       "      <td>37.3</td>\n",
       "      <td>104.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>74.0</td>\n",
       "      <td>7.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4300</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   surgery  age  hospital_number  rectal_temperature  pulse  respiratory_rate  \\\n",
       "0      2.0    1           530101                38.5   66.0              28.0   \n",
       "1      1.0    1           534817                39.2   88.0              20.0   \n",
       "2      2.0    1           530334                38.3   40.0              24.0   \n",
       "3      1.0    9          5290409                39.1  164.0              84.0   \n",
       "4      2.0    1           530255                37.3  104.0              35.0   \n",
       "\n",
       "   temperature_of_extremities  peripheral_pulse  mucous_membranes  \\\n",
       "0                         3.0               3.0               NaN   \n",
       "1                         NaN               NaN               4.0   \n",
       "2                         1.0               1.0               3.0   \n",
       "3                         4.0               1.0               6.0   \n",
       "4                         NaN               NaN               6.0   \n",
       "\n",
       "   capillary_refill_time  ...  packed_cell_volume  total_protein  \\\n",
       "0                    2.0  ...                45.0            8.4   \n",
       "1                    1.0  ...                50.0           85.0   \n",
       "2                    1.0  ...                33.0            6.7   \n",
       "3                    2.0  ...                48.0            7.2   \n",
       "4                    2.0  ...                74.0            7.4   \n",
       "\n",
       "   abdominocentesis_appearance  abdomcentesis_total_protein  outcome  \\\n",
       "0                          NaN                          NaN      2.0   \n",
       "1                          2.0                          2.0      3.0   \n",
       "2                          NaN                          NaN      1.0   \n",
       "3                          3.0                          5.3      2.0   \n",
       "4                          NaN                          NaN      2.0   \n",
       "\n",
       "   surgical_lesion  type_of_lesion_1  type_of_lesion_2  type_of_lesion_3  \\\n",
       "0                2             11300                 0                 0   \n",
       "1                2              2208                 0                 0   \n",
       "2                2                 0                 0                 0   \n",
       "3                1              2208                 0                 0   \n",
       "4                2              4300                 0                 0   \n",
       "\n",
       "   cp_data  \n",
       "0        2  \n",
       "1        2  \n",
       "2        1  \n",
       "3        1  \n",
       "4        2  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_horses = pd.read_csv('horse_data.csv', na_values='?', names=df_horses_col_names)\n",
    "df_horses[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "surgery - 0.3%\n",
      "age - 0.0%\n",
      "hospital_number - 0.0%\n",
      "rectal_temperature - 20.0%\n",
      "pulse - 8.0%\n",
      "respiratory_rate - 19.3%\n",
      "temperature_of_extremities - 18.7%\n",
      "peripheral_pulse - 23.0%\n",
      "mucous_membranes - 15.7%\n",
      "capillary_refill_time - 10.7%\n",
      "pain - 18.3%\n",
      "peristalsis - 14.7%\n",
      "abdominal_distension - 18.7%\n",
      "nasogastric_tube - 34.7%\n",
      "nasogastric_reflux - 35.3%\n",
      "nasogastric_reflux_PH - 82.3%\n",
      "rectal_examination - 34.0%\n",
      "abdomen - 39.3%\n",
      "packed_cell_volume - 9.7%\n",
      "total_protein - 11.0%\n",
      "abdominocentesis_appearance - 55.0%\n",
      "abdomcentesis_total_protein - 66.0%\n",
      "outcome - 0.3%\n",
      "surgical_lesion - 0.0%\n",
      "type_of_lesion_1 - 0.0%\n",
      "type_of_lesion_2 - 0.0%\n",
      "type_of_lesion_3 - 0.0%\n",
      "cp_data - 0.0%\n"
     ]
    }
   ],
   "source": [
    "# Смотрим на статистику пропущенных значений в каждом столбце\n",
    "for col_name in df_horses.columns:\n",
    "    pct_missing = df_horses[col_name].isnull().mean()\n",
    "    print(f'{col_name} - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Задание 1. Базовое изучение\n",
    "\n",
    "Изучить представленный набор данных на основе описания его столбцов и выбрать 8 столбцов для дальнейшего изучения (среди них должны быть как числовые, так и категориальные). Провести расчет базовых метрик для них, кратко описать результаты."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>530101</td>\n",
       "      <td>38.5</td>\n",
       "      <td>66.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>534817</td>\n",
       "      <td>39.2</td>\n",
       "      <td>88.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>530334</td>\n",
       "      <td>38.3</td>\n",
       "      <td>40.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>9</td>\n",
       "      <td>5290409</td>\n",
       "      <td>39.1</td>\n",
       "      <td>164.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>530255</td>\n",
       "      <td>37.3</td>\n",
       "      <td>104.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   surgery  age  hospital_number  rectal_temperature  pulse  respiratory_rate  \\\n",
       "0      2.0    1           530101                38.5   66.0              28.0   \n",
       "1      1.0    1           534817                39.2   88.0              20.0   \n",
       "2      2.0    1           530334                38.3   40.0              24.0   \n",
       "3      1.0    9          5290409                39.1  164.0              84.0   \n",
       "4      2.0    1           530255                37.3  104.0              35.0   \n",
       "\n",
       "   temperature_of_extremities  peripheral_pulse  \n",
       "0                         3.0               3.0  \n",
       "1                         NaN               NaN  \n",
       "2                         1.0               1.0  \n",
       "3                         4.0               1.0  \n",
       "4                         NaN               NaN  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Для выполнения обязательной части возьмем первые 8 столбцов\n",
    "df_horses_p1 = df_horses.iloc[:, 0:8]\n",
    "df_horses_p1_orig = deepcopy(df_horses_p1)\n",
    "df_horses_p1[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_df_base_stats(df):\n",
    "    \"\"\"\n",
    "    Функция для расчета базовых статистических методов датафрейма больных лошадей + мода\n",
    "    \n",
    "    Входные параметры:\n",
    "        df  -- датафрейм с больными лошадьми и корректными названиями столбцов\n",
    "        \n",
    "    Результат: датафрейм с базовой статистикой\n",
    "    \"\"\"\n",
    "    # Расчет базовых метрик, кроме моды\n",
    "    base_stat = df.describe()\n",
    "\n",
    "    # Добавим в датафрейм базовых показателей моду\n",
    "    mods = {}\n",
    "    for col_name in df.columns:\n",
    "        # Для ректальной температуры принято решение не округлять значения, так как нам важны десятые части\n",
    "        if col_name == 'rectal_temperature':\n",
    "            mods[col_name] = df[col_name].mode().values[0]\n",
    "        mods[col_name] = df[col_name].round().mode().values[0]\n",
    "    base_stat = pd.concat([base_stat, pd.DataFrame(mods, index=['mode'])])\n",
    "    \n",
    "    return base_stat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>299.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>3.000000e+02</td>\n",
       "      <td>240.000000</td>\n",
       "      <td>276.000000</td>\n",
       "      <td>242.000000</td>\n",
       "      <td>244.000000</td>\n",
       "      <td>231.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.397993</td>\n",
       "      <td>1.640000</td>\n",
       "      <td>1.085889e+06</td>\n",
       "      <td>38.167917</td>\n",
       "      <td>71.913043</td>\n",
       "      <td>30.417355</td>\n",
       "      <td>2.348361</td>\n",
       "      <td>2.017316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.490305</td>\n",
       "      <td>2.173972</td>\n",
       "      <td>1.529801e+06</td>\n",
       "      <td>0.732289</td>\n",
       "      <td>28.630557</td>\n",
       "      <td>17.642231</td>\n",
       "      <td>1.045054</td>\n",
       "      <td>1.042428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.184760e+05</td>\n",
       "      <td>35.400000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.289040e+05</td>\n",
       "      <td>37.800000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>18.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.303055e+05</td>\n",
       "      <td>38.200000</td>\n",
       "      <td>64.000000</td>\n",
       "      <td>24.500000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.347275e+05</td>\n",
       "      <td>38.500000</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>5.305629e+06</td>\n",
       "      <td>40.800000</td>\n",
       "      <td>184.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.275440e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery         age  hospital_number  rectal_temperature  \\\n",
       "count  299.000000  300.000000     3.000000e+02          240.000000   \n",
       "mean     1.397993    1.640000     1.085889e+06           38.167917   \n",
       "std      0.490305    2.173972     1.529801e+06            0.732289   \n",
       "min      1.000000    1.000000     5.184760e+05           35.400000   \n",
       "25%      1.000000    1.000000     5.289040e+05           37.800000   \n",
       "50%      1.000000    1.000000     5.303055e+05           38.200000   \n",
       "75%      2.000000    1.000000     5.347275e+05           38.500000   \n",
       "max      2.000000    9.000000     5.305629e+06           40.800000   \n",
       "mode     1.000000    1.000000     5.275440e+05           38.000000   \n",
       "\n",
       "            pulse  respiratory_rate  temperature_of_extremities  \\\n",
       "count  276.000000        242.000000                  244.000000   \n",
       "mean    71.913043         30.417355                    2.348361   \n",
       "std     28.630557         17.642231                    1.045054   \n",
       "min     30.000000          8.000000                    1.000000   \n",
       "25%     48.000000         18.500000                    1.000000   \n",
       "50%     64.000000         24.500000                    3.000000   \n",
       "75%     88.000000         36.000000                    3.000000   \n",
       "max    184.000000         96.000000                    4.000000   \n",
       "mode    48.000000         20.000000                    3.000000   \n",
       "\n",
       "       peripheral_pulse  \n",
       "count        231.000000  \n",
       "mean           2.017316  \n",
       "std            1.042428  \n",
       "min            1.000000  \n",
       "25%            1.000000  \n",
       "50%            2.000000  \n",
       "75%            3.000000  \n",
       "max            4.000000  \n",
       "mode           1.000000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Получаем базовую статистику для датафрейма\n",
    "display(get_df_base_stats(df_horses_p1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Заполненные значения столбца surgery корректны.\n",
      "ПРЕДУПРЕЖДЕНИЕ: Присутствуют некорректные данные в столбце age.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "9    24\n",
       "Name: age, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Заполненные значения столбца temperature_of_extremities корректны.\n",
      "Заполненные значения столбца peripheral_pulse корректны.\n"
     ]
    }
   ],
   "source": [
    "# Проверяем на корректность все категориальные индикаторы\n",
    "\n",
    "# Проверка корректности заполненных значений столбца surgery, значения должны принимать либо 1, либо 2\n",
    "if all(df_horses_p1['surgery'].dropna().isin([1,2])):\n",
    "    print('Заполненные значения столбца surgery корректны.')\n",
    "else:\n",
    "    print('ПРЕДУПРЕЖДЕНИЕ: Присутствуют некорректные данные в столбце surgery.')\n",
    "    display(df_horses_p1[~df_horses_p1['surgery'].isin([1,2])].surgery.value_counts())\n",
    "    \n",
    "# Проверка корректности заполненных значений столбца age, значения должны принимать либо 1, либо 2\n",
    "if all(df_horses_p1['age'].dropna().isin([1,2])):\n",
    "    print('Заполненные значения столбца age корректны.')\n",
    "else:\n",
    "    print('ПРЕДУПРЕЖДЕНИЕ: Присутствуют некорректные данные в столбце age.')\n",
    "    display(df_horses_p1[~df_horses_p1['age'].isin([1,2])].age.value_counts())\n",
    "    \n",
    "# Проверка корректности заполненных значений столбца temperature_of_extremities, значения должны принимать 1,2,3 или 4\n",
    "if all(df_horses_p1['temperature_of_extremities'].dropna().isin([1,2,3,4])):\n",
    "    print('Заполненные значения столбца temperature_of_extremities корректны.')\n",
    "else:\n",
    "    print('ПРЕДУПРЕЖДЕНИЕ: Присутствуют некорректные данные в столбце peripheral_pulse.')\n",
    "    (display(df_horses_p1[~df_horses_p1['temperature_of_extremities'].isin([1,2,3,4])]\n",
    "             .temperature_of_extremities.value_counts()))\n",
    "    \n",
    "# Проверка корректности заполненных значений столбца age, значения должны принимать 1,2,3 или 4\n",
    "if all(df_horses_p1['peripheral_pulse'].dropna().isin([1,2,3,4])):\n",
    "    print('Заполненные значения столбца peripheral_pulse корректны.')\n",
    "else:\n",
    "    print('ПРЕДУПРЕЖДЕНИЕ: Присутствуют некорректные данные в столбце peripheral_pulse.')\n",
    "    display(df_horses_p1[~df_horses_p1['peripheral_pulse'].isin([1,2,3,4])].peripheral_pulse.value_counts())  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Краткое описание результатов базовым показателей:\n",
    "\n",
    "**1. Атрибут surgery (факт наличия операции)**\n",
    "\n",
    "1 - Лошадь прооперирована  \n",
    "2 - Лечение прошло без хирургического вмешательства  \n",
    "\n",
    "Качественный, порядковый, заполненные данные корректны, процент незаполненных - 0.3%.  \n",
    "По показателю моды можно сказать, что большинство лошадей были прооперированы.\n",
    "\n",
    "**2. Атрибут Age (признак возраста)**\n",
    "\n",
    "1 - Взрослая лошадь  \n",
    "2 - Молодая лошадь (<6 месяцев)  \n",
    "\n",
    "Качественный, порядковый, часть данных заполнено некорректными значениями 9, число строк с некорректными значениями - 24, все строки заполнены.\n",
    "\n",
    "**3. Атрибут Hospital Number (номер больничного листа)**\n",
    "\n",
    "Качественный, номинальный, все строки заполнены.\n",
    "\n",
    "**4. Атрибут rectal temperature (ректальная температура)**\n",
    "\n",
    "- нормальная температура - 37.8\n",
    "- атрибут в градусах Цельсия\n",
    "\n",
    "Количественный,непрерывный, процент незаполненных - 20.0%\n",
    "\n",
    "По атрибутам min и max можно сказать, что серьезных выбросов и некорректных значений температуры нет. Также по моде, среднему значению и по 3 квартали можно сказать, что около половины лошадей имеют нормальную температуру с учетом того, что +-0.4 градуса от 37.8 тоже можно считать нормой. Так как разброс значений небольшой, средняя температура вполне отображает реальную действительность.\n",
    "\n",
    "У части лошадей испытывает поздний шок, так как температура понижена.\n",
    "\n",
    "**5. Атрибут pulsee (пульс)**\n",
    "\n",
    "Норма: 30-40 ударов в минуту, но у спортивных лошадей может иметь 20-25  \n",
    "Повышенный пульс сигнализирует о болезненных поражениях\n",
    "\n",
    "Количественный, дискретный, процент незаполненных - 8.0%\n",
    "\n",
    "По моде можно сказать, что наиболее встречаемая частота пульса - 48 ударов в минуту. Исходя из квартальных значений можно сказать, что как минимум половина лошадей имеют пульс отличный от нормы. При этом среднее значение не отображает реальной действительности, так как превосходит более половины значений. Есть выбросы в виде максимального значения в 184 удара в минуту, такие выбросы могут исказить статистику, при этом такой размер пульса вполне возможен.\n",
    "Также по минимальному размеру пульса можно сказать, что спортивных лошадей нет.\n",
    "\n",
    "**6. Атрибут respiratory rate (частота дыхания)**\n",
    "\n",
    "- нормальная оценка от 8 до 10\n",
    "- полезность сомнительна из-за больших колебаний\n",
    "\n",
    "Количественный, дискретный, процент незаполненных - 23.0%\n",
    "\n",
    "По моде можно сказать, что наиболее встречаемая частота дыхания - 20. При этотм как минимум 75% лошадений имеют отклонения от нормы по показателю.\n",
    "\n",
    "**7. Атрибут temperature of extremities (температура конечностей)**\n",
    "\n",
    "Возможные значения:  \n",
    "1 = Нормальный  \n",
    "2 = Горячий  \n",
    "3 = Прохладный  \n",
    "4 = Холодный  \n",
    "\n",
    "Качественный, порядковый\n",
    "   \n",
    "Прохладные и холодные конечности указывают на возможный шок. горячие конечности должны корелировать с ректальной температурой\n",
    "\n",
    "По моде можно сказать, что лошади с прохладными конечностями встречаются чаще всего, скорее всего они в шоковом состоянии. И судя по квартальным значениям таких лошадей не менее 1/4.\n",
    "\n",
    "**8. Атрибут peripheral pulse (периферический пульс)**\n",
    "\n",
    "возможные значения:  \n",
    "1 = нормальный    \n",
    "2 = повышенный  \n",
    "3 = пониженный  \n",
    "4 = отсутствует  \n",
    "\n",
    "Качественный, порядковый\n",
    "   \n",
    "Нормальный или повышенный п.п. указывают на адекватное кровообращение\n",
    "\n",
    "По моде можно сказать, что лошади с нормальным периферическим пульсом встречаются чаще всего, а по квартальным значениям можно сказать, что не менее половины лошадей имеют адекватное кровообращение, так как у них пульс либо нормальный, либо повышенный."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Задание 2. Работа с выбросами\n",
    "\n",
    "В выбранных числовых столбцах найти выбросы, выдвинуть гипотезы об их причинах и проинтерпретировать результаты. Принять и обосновать решение о дальнейшей работе с ними."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Анализ значений пульса"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3      164.0\n",
       "41     150.0\n",
       "55     160.0\n",
       "255    184.0\n",
       "275    150.0\n",
       "Name: pulse, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Найдем выбросы по формуле\n",
    "Q1 = df_horses_p1['pulse'].quantile(0.25)\n",
    "Q3 = df_horses_p1['pulse'].quantile(0.75)\n",
    "IQR = Q3 - Q1\n",
    "lower = Q1 - 1.5*IQR\n",
    "upper = Q3 + 1.5*IQR\n",
    "display(df_horses_p1[(df_horses_p1['pulse']>upper) | (df_horses_p1['pulse']<lower)]['pulse'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Исходя из квартальных значений видно, что 75% лошадей имеют пульс не более 88 ударов в минуту. А максимальный пульс - 184 удара в минуту. Так как значения от 90 и до 184 вполне реальны, то такие выбросы нельзя удалять. Можно попробовать разбить лошадей на 2 группы: \n",
    "\n",
    "- Группа 1 - лошади с пульсом до 100 ударов в минуту\n",
    "- Группа 2 - лошади с пульсом от 100 и выше ударов в минуту"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Процент лошадей из группы пульса 1 - 75.0%\n",
      "Процент лошадей из группы пульса 2 - 25.0%\n"
     ]
    }
   ],
   "source": [
    "df_horses_p1_pulse_1 = df_horses_p1[(df_horses_p1['pulse']<100)]\n",
    "df_horses_p1_pulse_2 = df_horses_p1[~(df_horses_p1['pulse']<100)]\n",
    "print(f'Процент лошадей из группы пульса 1 - {len(df_horses_p1_pulse_1)/len(df_horses_p1) :.1%}')\n",
    "print(f'Процент лошадей из группы пульса 2 - {len(df_horses_p1_pulse_2)/len(df_horses_p1) :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Посмотрим как изменились наши базовые статистические параметры"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Группа 1 - лошади с пульсом до 100 ударов в минуту\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "count    225.000000\n",
       "mean      61.168889\n",
       "std       17.258743\n",
       "min       30.000000\n",
       "25%       48.000000\n",
       "50%       60.000000\n",
       "75%       72.000000\n",
       "max       98.000000\n",
       "mode      48.000000\n",
       "Name: pulse, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print('Группа 1 - лошади с пульсом до 100 ударов в минуту')\n",
    "display(get_df_base_stats(df_horses_p1_pulse_1)['pulse'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Группа 2 - лошади с пульсом от 100 и выше ударов в минуту\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "count     51.000000\n",
       "mean     119.313725\n",
       "std       19.045724\n",
       "min      100.000000\n",
       "25%      104.000000\n",
       "50%      120.000000\n",
       "75%      128.500000\n",
       "max      184.000000\n",
       "mode     100.000000\n",
       "Name: pulse, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print('Группа 2 - лошади с пульсом от 100 и выше ударов в минуту')\n",
    "display(get_df_base_stats(df_horses_p1_pulse_2)['pulse'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Из полученных данных видно, что после деления лошадей на группы у нас выровналось значение среднего и стало практически равно медиане."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Анализ значений ректальной температуры\n",
    "\n",
    "Исходя из значений максимума, минимума, средней и медианы можно сказать об отсутствии выбросов в этих значениях."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    240.000000\n",
       "mean      38.167917\n",
       "std        0.732289\n",
       "min       35.400000\n",
       "25%       37.800000\n",
       "50%       38.200000\n",
       "75%       38.500000\n",
       "max       40.800000\n",
       "mode      38.000000\n",
       "Name: rectal_temperature, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1)['rectal_temperature'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Анализ значений частоты дыхания"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    242.000000\n",
       "mean      30.417355\n",
       "std       17.642231\n",
       "min        8.000000\n",
       "25%       18.500000\n",
       "50%       24.500000\n",
       "75%       36.000000\n",
       "max       96.000000\n",
       "mode      20.000000\n",
       "Name: respiratory_rate, dtype: float64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1)['respiratory_rate'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из базовой статистики, 75% лошадей имеют частоту дыхания не более 36. Как видно из статистики ниже в значения больше 40 попадают уже единицы лошадей и эти значения можно считать выбросами. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(8.0, 1),\n",
       " (9.0, 2),\n",
       " (10.0, 3),\n",
       " (12.0, 19),\n",
       " (13.0, 1),\n",
       " (14.0, 4),\n",
       " (15.0, 1),\n",
       " (16.0, 22),\n",
       " (18.0, 8),\n",
       " (20.0, 28),\n",
       " (21.0, 2),\n",
       " (22.0, 2),\n",
       " (23.0, 1),\n",
       " (24.0, 27),\n",
       " (25.0, 1),\n",
       " (26.0, 1),\n",
       " (28.0, 13),\n",
       " (30.0, 19),\n",
       " (32.0, 11),\n",
       " (34.0, 1),\n",
       " (35.0, 3),\n",
       " (36.0, 16),\n",
       " (40.0, 17),\n",
       " (42.0, 3),\n",
       " (44.0, 3),\n",
       " (48.0, 6),\n",
       " (50.0, 2),\n",
       " (51.0, 2),\n",
       " (52.0, 1),\n",
       " (58.0, 1),\n",
       " (60.0, 4),\n",
       " (66.0, 1),\n",
       " (68.0, 3),\n",
       " (70.0, 2),\n",
       " (72.0, 1),\n",
       " (80.0, 3),\n",
       " (84.0, 2),\n",
       " (88.0, 1),\n",
       " (90.0, 2),\n",
       " (96.0, 2)]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(df_horses_p1['respiratory_rate'].value_counts().items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Сама по себе метрика является сомнительной, так как у лошади могут быть большие перепады скорости дыхания в разные моменты времени. При этом отсутствуют данные, по которым можно найти кореляцию скорости дахыния (сжатие сосудов, уменьшение объема циркулирующей крови, дефицит кислорода). Поэтому эти данные можно исключить из анализа полностью."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Задание 3. Работа с пропусками\n",
    "\n",
    "Рассчитать количество выбросов для всех выбранных столбцов. Принять и обосновать решение о методе работы с пропусками по каждому столбцу, сформировать датафрейм, в котором пропуски будут отсутствовать."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля surgery (факт проведения операции)\n",
    "\n",
    "Процент пропущенных значений - 0.3%"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Для начала опередим зависимость факта операции от возраста. Как будет видно ниже, операции делают только старым лошадям."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Количество операций у молодых лошадей: 0\n"
     ]
    }
   ],
   "source": [
    "young_horse_surgery = len(df_horses_p1.loc[(df_horses_p1.surgery == 1) & (df_horses_p1.age == 2)])\n",
    "print(f'Количество операций у молодых лошадей: {young_horse_surgery}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Теперь требуется понять в какой ситуации старой лошади могут сделать операцию. Для этого посмотрим на стандартные показатели."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем стандартные метрики для старых лошадей, которым сделали операцию"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>pulse</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>8.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>8.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>21.000000</td>\n",
       "      <td>77.056794</td>\n",
       "      <td>44.275446</td>\n",
       "      <td>17.817437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>56.973678</td>\n",
       "      <td>46.347524</td>\n",
       "      <td>35.170418</td>\n",
       "      <td>44.130555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>24.055701</td>\n",
       "      <td>0.718764</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>37.125000</td>\n",
       "      <td>1.056200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>68.199324</td>\n",
       "      <td>38.092400</td>\n",
       "      <td>2.732283</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>101.000000</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>3.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>162.000000</td>\n",
       "      <td>148.000000</td>\n",
       "      <td>125.000000</td>\n",
       "      <td>127.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery       pulse  rectal_temperature  temperature_of_extremities\n",
       "count    8.000000    8.000000            8.000000                    8.000000\n",
       "mean    21.000000   77.056794           44.275446                   17.817437\n",
       "std     56.973678   46.347524           35.170418                   44.130555\n",
       "min      0.000000   24.055701            0.718764                    1.000000\n",
       "25%      1.000000   45.000000           37.125000                    1.056200\n",
       "50%      1.000000   68.199324           38.092400                    2.732283\n",
       "75%      1.000000  101.000000           39.000000                    3.250000\n",
       "max    162.000000  148.000000          125.000000                  127.000000\n",
       "mode     1.000000   24.000000           38.000000                    1.000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1.loc[(df_horses_p1.age == 1) & (df_horses_p1.surgery == 1), \n",
    "                 ['surgery', 'pulse', 'rectal_temperature', 'temperature_of_extremities']].describe()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем стандартные метрики для старых лошадей, которым не сделали операцию"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>pulse</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>8.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>8.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>15.625000</td>\n",
       "      <td>67.476820</td>\n",
       "      <td>40.480159</td>\n",
       "      <td>14.277933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>39.351665</td>\n",
       "      <td>35.839854</td>\n",
       "      <td>25.307199</td>\n",
       "      <td>34.653561</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>23.319323</td>\n",
       "      <td>0.762547</td>\n",
       "      <td>0.993463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>37.350000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>58.747619</td>\n",
       "      <td>38.189362</td>\n",
       "      <td>2.115000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>86.250000</td>\n",
       "      <td>39.025000</td>\n",
       "      <td>3.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>113.000000</td>\n",
       "      <td>130.000000</td>\n",
       "      <td>94.000000</td>\n",
       "      <td>100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery       pulse  rectal_temperature  temperature_of_extremities\n",
       "count    8.000000    8.000000            8.000000                    8.000000\n",
       "mean    15.625000   67.476820           40.480159                   14.277933\n",
       "std     39.351665   35.839854           25.307199                   34.653561\n",
       "min      0.000000   23.319323            0.762547                    0.993463\n",
       "25%      2.000000   45.000000           37.350000                    1.000000\n",
       "50%      2.000000   58.747619           38.189362                    2.115000\n",
       "75%      2.000000   86.250000           39.025000                    3.250000\n",
       "max    113.000000  130.000000           94.000000                  100.000000\n",
       "mode     2.000000   23.000000           38.000000                    1.000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1.loc[(df_horses_p1.age == 1) & (df_horses_p1.surgery == 2), \n",
    "                 ['surgery', 'pulse', 'rectal_temperature', 'temperature_of_extremities']].describe()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно в обоих ситуациях размер пульса и ректальной температуры не особо влияет на факт проведения операции, есть только определенная зависимость от температуры конечностей. Теперь посмотрим насколько больше старых лошадей с прохладными и холодными конечностями оперируют"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Количество лошадей с холодными коннечностями без операции\n",
    "len(df_horses_p1.loc[(df_horses_p1.age == 1) & \n",
    "                 (df_horses_p1.surgery == 2) & \n",
    "                 (df_horses_p1.temperature_of_extremities.isin([3,4])), \n",
    "                 ['surgery', 'pulse', 'rectal_temperature', 'temperature_of_extremities']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "78"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Количество прооперированных лошадей с холодными конечностями\n",
    "len(df_horses_p1.loc[(df_horses_p1.age == 1) & \n",
    "                 (df_horses_p1.surgery == 1) & \n",
    "                 (df_horses_p1.temperature_of_extremities.isin([3,4])), \n",
    "                 ['surgery', 'pulse', 'rectal_temperature', 'temperature_of_extremities']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "61.41732283464567"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "78*100/(78+49)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из расчета, 61% старых лошадей с холодными и прохладными конечностями попадают на операцию"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6141732283464567"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "78/(49+78)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "51"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Количество лошадей с нормальными и горячими конечностями без операции\n",
    "len(df_horses_p1.loc[(df_horses_p1.age == 1) & \n",
    "                 (df_horses_p1.surgery == 2) & \n",
    "                 (df_horses_p1.temperature_of_extremities.isin([1,2])), \n",
    "                 ['surgery', 'pulse', 'rectal_temperature', 'temperature_of_extremities']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(df_horses_p1.loc[(df_horses_p1.age == 1) & \n",
    "                 (df_horses_p1.surgery == 1) & \n",
    "                 (df_horses_p1.temperature_of_extremities.isin([1,2])), \n",
    "                 ['surgery', 'pulse', 'rectal_temperature', 'temperature_of_extremities']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из расчета, 49% старых лошадей с нормальными или горячими конечностями попадают на операцию. При этом если посмотреть на вероятности только нормальных или только горячих конечностей, то они сильно не отличаются от совокупности этих параметров.\n",
    "\n",
    "Таким образом выведем формулу заполнения пустого значения факта проведения операции.\n",
    "- молодым лошадям операцию не делают\n",
    "- если лошадть старая и у нее прохладные или теплые конечности, то на операцио она попадет с вероятностью 61%\n",
    "- если лошадь старая и у нее нормальные или горячие конечности, то на операцию 49%."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Применяем условие для молодых лошадей"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_horses_p1.loc[(df_horses_p1.surgery.isnull()) & (df_horses_p1.age == 2), 'surgery'] = 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Применяем условие для старых лошадей с прохладными и холодными конечностями"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Переменная с распределением значений 1 - 61% массива, 2 - 39% массива. Это число будет использовано для заполнения\n",
    "# отсутствующего значения случайным выбором\n",
    "values_for_random = list(np.full(61, 1)) + list(np.full(39, 2))\n",
    "\n",
    "df_horses_p1.loc[(df_horses_p1.surgery.isnull()) & \n",
    "                 (df_horses_p1.age == 1) &\n",
    "                 (df_horses_p1.temperature_of_extremities.isin([3,4])), 'surgery'] = random.choice(values_for_random)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Применяем условие для старых лошадей с нормальными и горячими конечностями"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Переменная с распределением значений 1 - 49% массива, 2 - 51% массива.\n",
    "values_for_random = list(np.full(49, 1)) + list(np.full(51, 2))\n",
    "df_horses_p1.loc[(df_horses_p1.surgery.isnull()) & \n",
    "                 (df_horses_p1.age == 1) &\n",
    "                 (df_horses_p1.temperature_of_extremities.isin([1,2])), 'surgery'] = random.choice(values_for_random)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в surgery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "surgery - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['surgery'].isnull().mean()\n",
    "print(f'surgery - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля Age (возраст)\n",
    "\n",
    "Все значения этой метрики заполнены, но есть некорректные значения в виде 9. \n",
    "\n",
    "Первое условие для замены нам уже известно - если лошадь оперировали, значит она точно старая. Требуется выяснить условия распределения возраста, если операции не было.\n",
    "\n",
    "Проверим количество молодых лошадей в нашем списке."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(df_horses_p1[df_horses_p1.age == 2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверим количество строк с некорректными данными"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9    24\n",
       "Name: age, dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_horses_p1[~df_horses_p1['age'].isin([1,2])].age.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из результата в данных в принципе нет молодых лошадей, поэтому можно предположить, что молодые лошади до 6 месяцев не болеют и так как ошибочных данных относительно немного, то такая замена не сильно повлияет на статистику. Поэтому заменяем все некорректные данные на значение 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_horses_p1.loc[~df_horses_p1['age'].isin([1,2]), 'age'] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['age'].isnull().mean()\n",
    "print(f'age - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля Hospital Number (номер больничгого листа)\n",
    "\n",
    "Все значения этой метрики заполнены"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hospital_number - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['hospital_number'].isnull().mean()\n",
    "print(f'hospital_number - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля rectal temperature (ректальная температура)\n",
    "\n",
    "Процент пропущенных значений - 18.7%"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из базовых параметров разброс значений температуры маленький, выбросов нет, среднее значение практически соответствует медиане. Поэтому отсутствующие значения можно бы заполнить средним, но мы не можем так сделать, так как наша температура делится на 2 категории: выше нормы и ниже нормы. В случае выбора среднего у нас у всех лошадей будет температура выше нормы."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    240.000000\n",
       "mean      38.167917\n",
       "std        0.732289\n",
       "min       35.400000\n",
       "25%       37.800000\n",
       "50%       38.200000\n",
       "75%       38.500000\n",
       "max       40.800000\n",
       "Name: rectal_temperature, dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_horses_p1['rectal_temperature'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Пробуем посмотреть как зависит температура от факта проведения операции"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1.0, 33), (2.0, 23)]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Смотрим статисктику прооперированных лошадей, у которых температура ниже нормы\n",
    "sorted(df_horses_p1[df_horses_p1['rectal_temperature'] < 37.8].surgery.value_counts().items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1.0, 97), (2.0, 70)]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Смотрим статисктику прооперированных лошадей, у которых температура выше нормы\n",
    "sorted(df_horses_p1[df_horses_p1['rectal_temperature'] > 37.8].surgery.value_counts().items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из статистики, если среди прооперированных и непрооперированных лошадей вероятность получить температуру выше нормы примерно в 3 раза больше, чем ниже нормы. Поэтому заполним температуру случайным значением из области температур выше и ниже нормы и учтем, что вероятность получить температуру выше нормы в 3 раза больше, чем получить температуру ниже нормы."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "low_t_range = np.arange(df_horses_p1.rectal_temperature.min(), 37.8, 0.1)\n",
    "high_t_range = np.arange(37.8, df_horses_p1.rectal_temperature.max(), 0.1)\n",
    "\n",
    "# Температуру будем заполнять случайным числом между min и max? учитываем, что вероятность получить температуру выше \n",
    "# нормы в три раза больше как у прооперированных, так и у лошадей без операций\n",
    "random_array = np.concatenate((low_t_range, high_t_range, high_t_range, high_t_range))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_horses_p1.loc[df_horses_p1.rectal_temperature.isnull(), 'rectal_temperature'] = random.choice(random_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в rectal_temperature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rectal_temperature - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['rectal_temperature'].isnull().mean()\n",
    "print(f'rectal_temperature - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля pulse (пульс)\n",
    "\n",
    "Процент пропущенных значений - 8.0%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    276.000000\n",
       "mean      71.913043\n",
       "std       28.630557\n",
       "min       30.000000\n",
       "25%       48.000000\n",
       "50%       64.000000\n",
       "75%       88.000000\n",
       "max      184.000000\n",
       "Name: pulse, dtype: float64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_horses_p1['pulse'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Подбираем оптимальный размер пульса, который можно считать выбросом\n",
    "len(df_horses_p1[df_horses_p1.pulse > 120].sort_values(by='pulse'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Так как в данных есть выбросы и 75% лошадей имеют пульс не более 88 ударов в минуту, а кол-во лошадей с пульсом более 120 всего 16, то будем считать значения больше 120 выбросами. Так как не удалось обнаружить какой-либо кореляции пульса между остальными данными во фрейме, то заполним пустые значения средними без учета выбросов."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_horses_p1.loc[df_horses_p1.pulse.isnull(), 'pulse'] = df_horses_p1[df_horses_p1.pulse < 120].pulse.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в pulse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pulse - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['pulse'].isnull().mean()\n",
    "print(f'pulse - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля respiratory rate (частота дыхания)\n",
    "\n",
    "Процент незаполненных - 23.0%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.0</td>\n",
       "      <td>3.000000e+02</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>242.000000</td>\n",
       "      <td>244.000000</td>\n",
       "      <td>231.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.396667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.085889e+06</td>\n",
       "      <td>37.634333</td>\n",
       "      <td>71.401920</td>\n",
       "      <td>30.417355</td>\n",
       "      <td>2.348361</td>\n",
       "      <td>2.017316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.490023</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.529801e+06</td>\n",
       "      <td>1.253512</td>\n",
       "      <td>27.512309</td>\n",
       "      <td>17.642231</td>\n",
       "      <td>1.045054</td>\n",
       "      <td>1.042428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.184760e+05</td>\n",
       "      <td>35.400000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.289040e+05</td>\n",
       "      <td>37.200000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>18.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.303055e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>65.524000</td>\n",
       "      <td>24.500000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.347275e+05</td>\n",
       "      <td>38.500000</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.305629e+06</td>\n",
       "      <td>40.800000</td>\n",
       "      <td>184.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.275440e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>66.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery    age  hospital_number  rectal_temperature       pulse  \\\n",
       "count  300.000000  300.0     3.000000e+02          300.000000  300.000000   \n",
       "mean     1.396667    1.0     1.085889e+06           37.634333   71.401920   \n",
       "std      0.490023    0.0     1.529801e+06            1.253512   27.512309   \n",
       "min      1.000000    1.0     5.184760e+05           35.400000   30.000000   \n",
       "25%      1.000000    1.0     5.289040e+05           37.200000   48.000000   \n",
       "50%      1.000000    1.0     5.303055e+05           38.000000   65.524000   \n",
       "75%      2.000000    1.0     5.347275e+05           38.500000   88.000000   \n",
       "max      2.000000    1.0     5.305629e+06           40.800000  184.000000   \n",
       "mode     1.000000    1.0     5.275440e+05           38.000000   66.000000   \n",
       "\n",
       "       respiratory_rate  temperature_of_extremities  peripheral_pulse  \n",
       "count        242.000000                  244.000000        231.000000  \n",
       "mean          30.417355                    2.348361          2.017316  \n",
       "std           17.642231                    1.045054          1.042428  \n",
       "min            8.000000                    1.000000          1.000000  \n",
       "25%           18.500000                    1.000000          1.000000  \n",
       "50%           24.500000                    3.000000          2.000000  \n",
       "75%           36.000000                    3.000000          3.000000  \n",
       "max           96.000000                    4.000000          4.000000  \n",
       "mode          20.000000                    3.000000          1.000000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Так как не удалось найти зависимость между частотой дыхания и остальными метриками, а так же сами авторы дата сета утверждают о бесполезности данных в этом столбце, заполним пустые значения медианой из датасета без выбросов."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Найдем выбросы по формуле\n",
    "Q1 = df_horses_p1['respiratory_rate'].quantile(0.25)\n",
    "Q3 = df_horses_p1['respiratory_rate'].quantile(0.75)\n",
    "IQR = Q3 - Q1\n",
    "lower = Q1 - 1.5*IQR\n",
    "upper = Q3 + 1.5*IQR\n",
    "# Находим медианное значение без выбросов\n",
    "respiratory_rate_median = df_horses_p1[(df_horses_p1['respiratory_rate'] < upper) & \n",
    "                                        (df_horses_p1['respiratory_rate'] > lower)]['respiratory_rate'].median()\n",
    "df_horses_p1.loc[df_horses_p1.respiratory_rate.isnull(), 'respiratory_rate'] = respiratory_rate_median"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в respiratory_rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "respiratory_rate - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['respiratory_rate'].isnull().mean()\n",
    "print(f'respiratory_rate - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля temperature of extremities (частота дыхания)\n",
    "\n",
    "Процент незаполненных - 18.7%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.0</td>\n",
       "      <td>3.000000e+02</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>244.000000</td>\n",
       "      <td>231.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.396667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.085889e+06</td>\n",
       "      <td>37.634333</td>\n",
       "      <td>71.401920</td>\n",
       "      <td>29.176667</td>\n",
       "      <td>2.348361</td>\n",
       "      <td>2.017316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.490023</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.529801e+06</td>\n",
       "      <td>1.253512</td>\n",
       "      <td>27.512309</td>\n",
       "      <td>16.041088</td>\n",
       "      <td>1.045054</td>\n",
       "      <td>1.042428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.184760e+05</td>\n",
       "      <td>35.400000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.289040e+05</td>\n",
       "      <td>37.200000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.303055e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>65.524000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.347275e+05</td>\n",
       "      <td>38.500000</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>34.250000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.305629e+06</td>\n",
       "      <td>40.800000</td>\n",
       "      <td>184.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.275440e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>66.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery    age  hospital_number  rectal_temperature       pulse  \\\n",
       "count  300.000000  300.0     3.000000e+02          300.000000  300.000000   \n",
       "mean     1.396667    1.0     1.085889e+06           37.634333   71.401920   \n",
       "std      0.490023    0.0     1.529801e+06            1.253512   27.512309   \n",
       "min      1.000000    1.0     5.184760e+05           35.400000   30.000000   \n",
       "25%      1.000000    1.0     5.289040e+05           37.200000   48.000000   \n",
       "50%      1.000000    1.0     5.303055e+05           38.000000   65.524000   \n",
       "75%      2.000000    1.0     5.347275e+05           38.500000   88.000000   \n",
       "max      2.000000    1.0     5.305629e+06           40.800000  184.000000   \n",
       "mode     1.000000    1.0     5.275440e+05           38.000000   66.000000   \n",
       "\n",
       "       respiratory_rate  temperature_of_extremities  peripheral_pulse  \n",
       "count        300.000000                  244.000000        231.000000  \n",
       "mean          29.176667                    2.348361          2.017316  \n",
       "std           16.041088                    1.045054          1.042428  \n",
       "min            8.000000                    1.000000          1.000000  \n",
       "25%           20.000000                    1.000000          1.000000  \n",
       "50%           24.000000                    3.000000          2.000000  \n",
       "75%           34.250000                    3.000000          3.000000  \n",
       "max           96.000000                    4.000000          4.000000  \n",
       "mode          24.000000                    3.000000          1.000000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Попробуем проверить как зависит температура конечностей от ректальной температуры, найдем количество каждого значения temperature_of_extremities для следующих диапазонов:\n",
    "- ректальная температура меньше 37.8\n",
    "- ректальная температура от 37.8 до 39\n",
    "- ректальная температура от 39 и выше"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1.0, 18), (2.0, 10), (3.0, 52), (4.0, 10)]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ректальная температура меньше 37.8\n",
    "sorted(df_horses_p1.loc[(df_horses_p1.rectal_temperature < 37.8), \n",
    "                        'temperature_of_extremities'].value_counts().items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1.0, 57), (2.0, 17), (3.0, 45), (4.0, 11)]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ректальная температура от 37.8 до 39\n",
    "sorted(df_horses_p1.loc[(df_horses_p1.rectal_temperature >= 37.8) &\n",
    "                        (df_horses_p1.rectal_temperature < 39), \n",
    "                        'temperature_of_extremities'].value_counts().items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1.0, 3), (2.0, 3), (3.0, 12), (4.0, 6)]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ректальная температура от 39 и выше\n",
    "sorted(df_horses_p1.loc[(df_horses_p1.rectal_temperature >= 39), \n",
    "                        'temperature_of_extremities'].value_counts().items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Как видно из полученных диапазонов во всех группах преобладает значение 3. Так как не удалось найти иной зависимости температуры конечностей от других данных, имеет смысл заполнить пустые значения модой со значением 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_horses_p1.loc[df_horses_p1.temperature_of_extremities.isnull(), 'temperature_of_extremities'] = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в temperature_of_extremities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "temperature_of_extremities - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['temperature_of_extremities'].isnull().mean()\n",
    "print(f'temperature_of_extremities - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Заполнение данными поля peripheral pulse (периферический пульс)\n",
    "\n",
    "Процент незаполненных - 23.0%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.0</td>\n",
       "      <td>3.000000e+02</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>231.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.396667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.085889e+06</td>\n",
       "      <td>37.634333</td>\n",
       "      <td>71.401920</td>\n",
       "      <td>29.176667</td>\n",
       "      <td>2.470000</td>\n",
       "      <td>2.017316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.490023</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.529801e+06</td>\n",
       "      <td>1.253512</td>\n",
       "      <td>27.512309</td>\n",
       "      <td>16.041088</td>\n",
       "      <td>0.975845</td>\n",
       "      <td>1.042428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.184760e+05</td>\n",
       "      <td>35.400000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.289040e+05</td>\n",
       "      <td>37.200000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.303055e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>65.524000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.347275e+05</td>\n",
       "      <td>38.500000</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>34.250000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.305629e+06</td>\n",
       "      <td>40.800000</td>\n",
       "      <td>184.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.275440e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>66.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery    age  hospital_number  rectal_temperature       pulse  \\\n",
       "count  300.000000  300.0     3.000000e+02          300.000000  300.000000   \n",
       "mean     1.396667    1.0     1.085889e+06           37.634333   71.401920   \n",
       "std      0.490023    0.0     1.529801e+06            1.253512   27.512309   \n",
       "min      1.000000    1.0     5.184760e+05           35.400000   30.000000   \n",
       "25%      1.000000    1.0     5.289040e+05           37.200000   48.000000   \n",
       "50%      1.000000    1.0     5.303055e+05           38.000000   65.524000   \n",
       "75%      2.000000    1.0     5.347275e+05           38.500000   88.000000   \n",
       "max      2.000000    1.0     5.305629e+06           40.800000  184.000000   \n",
       "mode     1.000000    1.0     5.275440e+05           38.000000   66.000000   \n",
       "\n",
       "       respiratory_rate  temperature_of_extremities  peripheral_pulse  \n",
       "count        300.000000                  300.000000        231.000000  \n",
       "mean          29.176667                    2.470000          2.017316  \n",
       "std           16.041088                    0.975845          1.042428  \n",
       "min            8.000000                    1.000000          1.000000  \n",
       "25%           20.000000                    1.000000          1.000000  \n",
       "50%           24.000000                    3.000000          2.000000  \n",
       "75%           34.250000                    3.000000          3.000000  \n",
       "max           96.000000                    4.000000          4.000000  \n",
       "mode          24.000000                    3.000000          1.000000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(get_df_base_stats(df_horses_p1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Так как не удалось найти определённой зависимости периферического пульса от других данных, имеет смысл заполнить пустые значения модой со значением 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_horses_p1.loc[df_horses_p1.peripheral_pulse.isnull(), 'peripheral_pulse'] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проверяем процент незаполненных полей в temperature_of_extremities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "temperature_of_extremities - 0.0%\n"
     ]
    }
   ],
   "source": [
    "pct_missing = df_horses_p1['temperature_of_extremities'].isnull().mean()\n",
    "print(f'temperature_of_extremities - {pct_missing :.1%}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Итоговые результаты"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>299.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>3.000000e+02</td>\n",
       "      <td>240.000000</td>\n",
       "      <td>276.000000</td>\n",
       "      <td>242.000000</td>\n",
       "      <td>244.000000</td>\n",
       "      <td>231.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.397993</td>\n",
       "      <td>1.640000</td>\n",
       "      <td>1.085889e+06</td>\n",
       "      <td>38.167917</td>\n",
       "      <td>71.913043</td>\n",
       "      <td>30.417355</td>\n",
       "      <td>2.348361</td>\n",
       "      <td>2.017316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.490305</td>\n",
       "      <td>2.173972</td>\n",
       "      <td>1.529801e+06</td>\n",
       "      <td>0.732289</td>\n",
       "      <td>28.630557</td>\n",
       "      <td>17.642231</td>\n",
       "      <td>1.045054</td>\n",
       "      <td>1.042428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.184760e+05</td>\n",
       "      <td>35.400000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.289040e+05</td>\n",
       "      <td>37.800000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>18.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.303055e+05</td>\n",
       "      <td>38.200000</td>\n",
       "      <td>64.000000</td>\n",
       "      <td>24.500000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.347275e+05</td>\n",
       "      <td>38.500000</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>5.305629e+06</td>\n",
       "      <td>40.800000</td>\n",
       "      <td>184.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>5.275440e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery         age  hospital_number  rectal_temperature  \\\n",
       "count  299.000000  300.000000     3.000000e+02          240.000000   \n",
       "mean     1.397993    1.640000     1.085889e+06           38.167917   \n",
       "std      0.490305    2.173972     1.529801e+06            0.732289   \n",
       "min      1.000000    1.000000     5.184760e+05           35.400000   \n",
       "25%      1.000000    1.000000     5.289040e+05           37.800000   \n",
       "50%      1.000000    1.000000     5.303055e+05           38.200000   \n",
       "75%      2.000000    1.000000     5.347275e+05           38.500000   \n",
       "max      2.000000    9.000000     5.305629e+06           40.800000   \n",
       "mode     1.000000    1.000000     5.275440e+05           38.000000   \n",
       "\n",
       "            pulse  respiratory_rate  temperature_of_extremities  \\\n",
       "count  276.000000        242.000000                  244.000000   \n",
       "mean    71.913043         30.417355                    2.348361   \n",
       "std     28.630557         17.642231                    1.045054   \n",
       "min     30.000000          8.000000                    1.000000   \n",
       "25%     48.000000         18.500000                    1.000000   \n",
       "50%     64.000000         24.500000                    3.000000   \n",
       "75%     88.000000         36.000000                    3.000000   \n",
       "max    184.000000         96.000000                    4.000000   \n",
       "mode    48.000000         20.000000                    3.000000   \n",
       "\n",
       "       peripheral_pulse  \n",
       "count        231.000000  \n",
       "mean           2.017316  \n",
       "std            1.042428  \n",
       "min            1.000000  \n",
       "25%            1.000000  \n",
       "50%            2.000000  \n",
       "75%            3.000000  \n",
       "max            4.000000  \n",
       "mode           1.000000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Кол-во пропусков: 268\n"
     ]
    }
   ],
   "source": [
    "# Исходные данные\n",
    "display(get_df_base_stats(df_horses_p1_orig))\n",
    "print(f'Кол-во пропусков: {sum(len(df_horses_p1_orig) - df_horses_p1_orig.count())}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>surgery</th>\n",
       "      <th>age</th>\n",
       "      <th>hospital_number</th>\n",
       "      <th>rectal_temperature</th>\n",
       "      <th>pulse</th>\n",
       "      <th>respiratory_rate</th>\n",
       "      <th>temperature_of_extremities</th>\n",
       "      <th>peripheral_pulse</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.0</td>\n",
       "      <td>3.000000e+02</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>300.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.396667</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.085889e+06</td>\n",
       "      <td>37.634333</td>\n",
       "      <td>71.401920</td>\n",
       "      <td>29.176667</td>\n",
       "      <td>2.470000</td>\n",
       "      <td>1.783333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.490023</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.529801e+06</td>\n",
       "      <td>1.253512</td>\n",
       "      <td>27.512309</td>\n",
       "      <td>16.041088</td>\n",
       "      <td>0.975845</td>\n",
       "      <td>1.009846</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.184760e+05</td>\n",
       "      <td>35.400000</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.289040e+05</td>\n",
       "      <td>37.200000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.303055e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>65.524000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.347275e+05</td>\n",
       "      <td>38.500000</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>34.250000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.305629e+06</td>\n",
       "      <td>40.800000</td>\n",
       "      <td>184.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mode</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.275440e+05</td>\n",
       "      <td>38.000000</td>\n",
       "      <td>66.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          surgery    age  hospital_number  rectal_temperature       pulse  \\\n",
       "count  300.000000  300.0     3.000000e+02          300.000000  300.000000   \n",
       "mean     1.396667    1.0     1.085889e+06           37.634333   71.401920   \n",
       "std      0.490023    0.0     1.529801e+06            1.253512   27.512309   \n",
       "min      1.000000    1.0     5.184760e+05           35.400000   30.000000   \n",
       "25%      1.000000    1.0     5.289040e+05           37.200000   48.000000   \n",
       "50%      1.000000    1.0     5.303055e+05           38.000000   65.524000   \n",
       "75%      2.000000    1.0     5.347275e+05           38.500000   88.000000   \n",
       "max      2.000000    1.0     5.305629e+06           40.800000  184.000000   \n",
       "mode     1.000000    1.0     5.275440e+05           38.000000   66.000000   \n",
       "\n",
       "       respiratory_rate  temperature_of_extremities  peripheral_pulse  \n",
       "count        300.000000                  300.000000        300.000000  \n",
       "mean          29.176667                    2.470000          1.783333  \n",
       "std           16.041088                    0.975845          1.009846  \n",
       "min            8.000000                    1.000000          1.000000  \n",
       "25%           20.000000                    1.000000          1.000000  \n",
       "50%           24.000000                    3.000000          1.000000  \n",
       "75%           34.250000                    3.000000          3.000000  \n",
       "max           96.000000                    4.000000          4.000000  \n",
       "mode          24.000000                    3.000000          1.000000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Кол-во пропусков: 0\n"
     ]
    }
   ],
   "source": [
    "# Результирующие данные\n",
    "display(get_df_base_stats(df_horses_p1))\n",
    "print(f'Кол-во пропусков: {sum(len(df_horses_p1) - df_horses_p1.count())}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
